package org.code.clientemovil;

import org.code.servicioWeb.Conexion;
import org.code.servicioWeb.Restaurante;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.Toast;

public class Comentar extends Activity {
	private EditText txtTexto;
	private RatingBar estrellas;
	private int puntuacion;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.comentar);

		ImageView logo = (ImageView)findViewById(R.id.imageView2);
		logo.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0)
			{
				Intent intent = new Intent(Comentar.this, MainActivity.class);
				startActivity(intent);
			}
		});
		
		Button enviar = (Button)findViewById(R.id.button1);
		estrellas = (RatingBar)findViewById(R.id.ratingBar1);
		estrellas.setStepSize(1);

		enviar.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0)
			{
				puntuacion = (int)estrellas.getRating();
				txtTexto = (EditText)findViewById(R.id.editTextComentar);
				String texto = txtTexto.getText().toString();

				Conexion conexion = new Conexion();
				Restaurante rest = VerRestaurante.restauranteActual;
				if(conexion.comentar(rest.getIdRestaurante(), MainActivity.usuario, puntuacion, texto)==true)
				{
					Intent intent = new Intent(Comentar.this, VerRestaurante.class);
					startActivity(intent);
				}		
				else
				{
					Intent intent = new Intent(Comentar.this, VerRestaurante.class);
					startActivity(intent);
					Toast.makeText(getApplicationContext(),"No se ha podido publicar el comentario",Toast.LENGTH_SHORT).show();
				}
			}
		});
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
}
